package com.jifeng.br.pjj.bookreader.controller.admin;


import com.jifeng.br.pjj.bookreader.entity.UserinfoEntity;
import com.jifeng.br.pjj.bookreader.service.UserinfoService;
import com.jifeng.br.pjj.bookreader.util.R;
import com.jifeng.br.pjj.bookreader.util.form.PasswordForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户登录表 前端控制器
 * </p>
 *
 * @author 机蜂
 * @since 2023-07-11
 */
@RestController
@RequestMapping("/userinfo")
public class UserinfoController extends BaseController {
    @Autowired
    private UserinfoService userinfoService;

    @GetMapping("/page")
    public R page(int page, int limit, UserinfoEntity userinfo) {
        return R.data(userinfoService.getPage(page, limit, userinfo));
    }

    @GetMapping("/info")
    public R info() {
        return R.data(getUser());
    }

    @GetMapping("/info/{userId}")
    public R info(@PathVariable Integer userId) {
//        return R.ok().put("data",userinfoService.getById(userId));
        return R.data(userinfoService.getById(userId));
    }

    @PostMapping("/update")
    public R update(@RequestBody UserinfoEntity userinfo) {
        userinfoService.update(userinfo);
        return R.ok();
    }

    //    @PostMapping("/password")
    @RequestMapping(value = "/password", method = RequestMethod.POST)
    public R password(@RequestBody PasswordForm passwordForm) {
        userinfoService.updatePassword(passwordForm);
        return R.ok();
    }

    @DeleteMapping("/delete/{userId}")
    public R delete(@PathVariable Integer userId) {
        if (userId == 1) {
            return R.error("超级管理员禁止删除！");
        }
        userinfoService.removeById(userId);
        return R.ok();
    }

}

